<template>
	<view class="u-page">
		<u-collapse :value="[]">
			<u-collapse-item name="1" title="工程信息" icon="https://cdn.uviewui.com/uview/example/cell.png">
		
		<buildinfo :buildid='buildid'></buildinfo>
		</u-collapse-item></u-collapse>
		<u-picker :show="show" :columns="columns" @cancel="show=false" @confirm="radioChange"></u-picker>
		<u-picker :show="isSelectingUser"  @cancel="isSelectingUser=false" :columns="[userlist]" keyName="Name" @confirm="userselected"></u-picker>

		<!-- <u-search placeholder="请输入关键字" v-model="keyword"></u-search> -->
		<u-form labelWidth='100'>
			<u-form-item :borderBottom='true' label="类型" >
				<u-radio-group v-model="teamtype" @change="getlist">
					<u-radio name="施工队" label="施工队"></u-radio>
					<u-radio name="协作单位" label="协作单位"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item v-if='teamtype=="施工队"' :borderBottom='true' label="劳务公司"  @change="getlist">
				<u-radio-group v-model="workcompany" @change="getlist" placement="column">
					<u-radio name="百邦劳务有限公司" label="百邦劳务有限公司"></u-radio>
					<u-radio name="中通文博技术服务有限公司" label="中通文博技术服务有限公司"></u-radio>
					<u-radio name="湖北赢在起点企业管理有限公司" label="湖北赢在起点企业管理有限公司"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item :borderBottom='true' label="地市" >
				<u-text :text="city" @click='show=true' @change="getlist"></u-text>
				<!-- 		<u-radio-group v-model="city" placement="column">
					<u-radio name="南宁" label="南宁"></u-radio>
					<u-radio name="柳州" label="柳州"></u-radio>
					<u-radio name="桂林" label="桂林"></u-radio>
					<u-radio name="梧州" label="梧州"></u-radio>
					<u-radio name="北海" label="北海"></u-radio>
					<u-radio name="崇左" label="崇左"></u-radio>
					<u-radio name="来宾" label="来宾"></u-radio>
					<u-radio name="贺州" label="贺州"></u-radio>
					<u-radio name="玉林" label="玉林"></u-radio>
					<u-radio name="百色" label="百色"></u-radio>
					<u-radio name="河池" label="河池"></u-radio>
					<u-radio name="钦州" label="钦州"></u-radio>
					<u-radio name="防城港" label="防城港"></u-radio>
					<u-radio name="贵港" label="贵港"></u-radio>
				</u-radio-group> -->
			</u-form-item>
		</u-form>
		<u-search v-model="keyword" :show-action="false" @change="searchlist" placeholder="请输入关键字"></u-search>
		<u-list   @scrolltolower="scrolltolower">
			<u-list-item v-for="(item, index) in buildlist" :key="index">
				<u-cell v-if="teamtype=='施工队'" :title="item.Name" isLink @click="ok"
					:label="`折扣率:${item.CutRateUp*100}% 备注:${item.Remark || ''}`" :name="item"
					icon="https://cdn.uviewui.com/uview/example/tag.png">
				</u-cell>
				<u-cell v-else :title="item.Name" isLink @click="ok"
					:label="`折扣率:${item.CutRateUp*100}% 联系人:${item.Contacts} 备注:${item.Remark || ''}`" :name="item"
					icon="https://cdn.uviewui.com/uview/example/tag.png">
				</u-cell>
			</u-list-item>
			<u-loadmore :status="status" v-if="loading" />
		</u-list>

			<!-- <u-loadmore :status="status" v-if="loading" /> -->
<!-- 					<u-popup style="height: 50vh;" :show="isSelectingUser" @close="isSelectingUser=false" @open="openpage" :closeable	="true">
		<u-list v-if="isSelectingUser">
			<u-list-item v-for="(item, index) in userlist" :key="index">
				<u-cell :title="item.Name" isLink @click="openpage"
					:label="`部门:${item.Dept.Name} 职务:${item.Duty || ''}`" :name="item"
					icon="https://cdn.uviewui.com/uview/example/tag.png">
				</u-cell>
			</u-list-item></u-list>
					</u-popup> -->
		
		<u-modal :show="showOK" title="派工" content='确认派工？' :showCancelButton="true" @cancel="showOK=false"
			@confirm="confirm"></u-modal>
			<u-toast ref="uToast"></u-toast>
			<u-loading-page loading-text="提交中..." :loading="loading2"></u-loading-page>
	</view>
</template>

<script>
	import {
		getworkteamlist,
		SendWork,
		GetByDeptName,
	} from '@/common/api.js'
	import buildinfo from "../c_buildinfo.vue"
	export default {
		components: {
		  buildinfo
		},
		data() {
			return {
				loading2:false,
				userlist: [],
				isSelectingUser: false,
				showOK: false,
				columns: [("南宁,柳州,桂林,梧州,北海,崇左,来宾,贺州,玉林,百色,河池,钦州,防城,贵港").split(',')],
				show: false,
				buildlist: [],
				keyword: "",
				pageIndex: 1, //当前页数
				pageSize: 20, //页数大小
				totalPage: "", //总页数
				total: 0, //数据总数
				status: 'loadmore', //加载状态
				loading: true,
				city: "",
				teamtype: "施工队",
				workcompany: "百邦劳务有限公司",
				buildid: '',
				user: JSON.parse(uni.getStorageSync("$userinfo")),
				selectedTeam: {},
				selectedUserId:'',

			}
		},
		onLoad(opt) {
			// this.columns = ("南宁,柳州,桂林").split(',')
			this.buildid = opt.buildid
			this.city = opt.city.replace('市', '')
			this.getlist()
		},
		methods: {
			userselected(e)
			{
				this.showOK=true;
				this.selectedUserId= e.value[0].Id
				
			},
			confirm(e) {
				this.isSelectingUser=false
				// if(e)
				// {
				// 	this.selectedUserId= e.value[0].Id
				// }
				this.loading2=true
				this.showOK=false
				SendWork([this.buildid], this.selectedUserId,this.selectedTeam.Id).then(res => {
					
					this.loading2=false
									this.$refs.uToast.show({
																type: 'success',
																title: '派工完成',
																message: res.message,
										complete() {
											uni.navigateBack()
										}
									})
				})
			},
			radioChange(e) {
				this.city = e.value[0]
				this.show = false
				this.buildlist=[]
				this.getlist()
			},
			openpage() {//部门没人
				this.showOK = true
				this.selectedUserId = ''
			},
			ok(e) {
		
		        this.selectedTeam = e.name
				GetByDeptName(this.selectedTeam.Name).then(res => {
					this.userlist = res;
					if (this.teamtype!="施工队") {
						this.isSelectingUser = true
					} else //部门没人就直接提交
					{
						this.openpage()
					}
				})

			},

			async getlist(notclear) {
				if (notclear)
					this.buildlist = []
				// let querytype = this.$route.query.querytype
				let para = {
					Parameters: [],
				}
				if (this.keyword != "") {
					para.Parameters.push({
						FieldName: "Name",
						ConditionalType: "Like",
						FieldValue: this.keyword
					})

				}
				para.Parameters.push({
					FieldName: "TeamType",
					ConditionalType: "Equal",
					FieldValue: this.teamtype,
				})
				para.Parameters.push({
					FieldName: "City",
					ConditionalType: "Equal",
					FieldValue: this.city,
				})
				if (this.teamtype == '施工队')
					para.Parameters.push({
						FieldName: "WorkCompany",
						ConditionalType: "Equal",
						FieldValue: this.workcompany,
					})
				// para.OrderBys = ['CreateTime desc']
				para.pageSize = this.pageSize
				para.pageIndex = this.pageIndex
				// if (querytype == "日志") {


				// }
				await getworkteamlist(para).then(res => {
					let data = res.data
					this.buildlist.push(...data)
					this.total = res.count
					this.totalPage = Math.ceil(this.total / this.pageSize)
				})
			},
			scrolltolower() {
				// 如果当前页数大于等于总页数，状态修改为没有更多了，不再继续往下执行代码
				if (this.pageIndex >= this.totalPage) {
					this.status = 'nomore';
					return;
				};
				this.status = 'loading'; //状态改为加载中
				this.pageIndex = ++this.pageIndex; //页面新增一页
				this.getlist(true); //修改页数后，重新获取数据
			},
			async searchlist() {
				this.buildlist = [] //初始化
				this.pageIndex = 1 //初始化当前页数为1
				this.loading = true
				await this.getlist()
				if (this.buildlist.length < this.pageSize) {
					this.loading = false; //如果数据列表小于当前页数，就让加载标签消失
				}
			}
		},
	}
</script>

<style>

</style>